package org.codeart.singleton;

/**
 * 懒汉式，线程安全
 * 同步锁缺点：串行操作导致并发度比较低
 */
public class Singleton03 {

    private static Singleton03 instance;

    private Singleton03() {

    }

    // 添加同步锁，保证多线程模式下单例模式线程安全
    public static synchronized Singleton03 getInstance() {
        return instance == null ? instance = new Singleton03() : instance;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 50; i++) {
            new Thread(() -> {
                Singleton03 s = getInstance();
                System.out.println(Thread.currentThread().getName() + ": " + s);
            }).start();
        }
    }
}
